package Structural_design_pattern.Composite_Pattern.Example1;

import java.util.ArrayList;
import java.util.List;

public class Menu extends MenuComponent{
    private List<MenuComponent> menuComponentList = new ArrayList<MenuComponent>();
    
    public Menu(String name, int level){
        this.name = name;
        this.level = level;
    }
    
    public void add(MenuComponent menuComponent){
        menuComponentList.add(menuComponent);
    }
    
    public void remove(MenuComponent menuComponent){
        menuComponentList.remove(menuComponent);
    }
    
    public MenuComponent getChild(int i){
        return menuComponentList.get(i);
    }
    
    @Override
    public void print() {
        // 打印菜单名称
        for (int i = 0; i < level; i++) {
            System.out.print("--");
        }
        System.out.println(name);
        
        // 打印子菜单项的名称（使用递归）
        for (MenuComponent component : menuComponentList) {
            component.print();
        }
    }
}
